package com.phpxiu.app.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;


/**
 * 警告提示
 * Created by WuJinZhou on 2015/8/8.
 */
public class Alert extends Dialog implements View.OnClickListener {
    private TextView msgTv;
    private Button ok;
    private View.OnClickListener listener;

    public Alert(Context context) {
        super(context,R.style.interactiveDialog);
        setContentView(R.layout.alert);
    }

    @Override
    public void setContentView(int layoutResId) {
        if(layoutResId!=0) {
            super.setContentView(layoutResId);
            findViewById(R.id.root_view).setOnClickListener(this);
            msgTv = (TextView) findViewById(R.id.alert_msg);
            ok = (Button) findViewById(R.id.alert_ok);
            ok.setOnClickListener(this);
        }
    }

    /**
     * 设置提示消息
     * @param msg 提示消息
     */
    public void setMsg(String msg){
        if(msgTv!=null){
            msgTv.setText(msg);
        }
    }

    public void setOnClickListener(View.OnClickListener listener) {
        this.listener = listener;
        if(listener!=null) {
            setCanceledOnTouchOutside(false);
        }else{
            setCanceledOnTouchOutside(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.root_view:
                if(listener!=null){//有监听，则必须点确定才能退出
                    return;
                }
                break;
            case R.id.alert_ok:
                if(listener!=null){
                    listener.onClick(v);
                }
                break;
        }
        dismiss();
    }

}
